package com.demos.webcommon.事务;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("TransactionController")
@RequestMapping("transaction")
public class controller {
    @Autowired
    private service service;

    /**
     * 普通回滚
     * @param name
     * @return
     */
    @GetMapping("add1/{name}")
    public int add1(@PathVariable String name){
        return service.doAdd1(name);
    }

    /**
     * 自调方法回滚失败
     * @param name
     * @return
     */
    @GetMapping("add2/{name}")
    public int add2(@PathVariable String name){
        return service.doAdd2(name);
    }

    /**
     * 有报错的事务回滚
     * @param name
     * @return
     */
    @GetMapping("add3/{name}")
    public int add3(@PathVariable String name){
        int i = 0;
        i += service.privateAdd1(name);
        i += service.privateAdd2(name);
        return i;
    }

    /**
     * 根据传播特性（SUPPORTS支持各自独立的事务--同上）
     * @param name
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @GetMapping("add4/{name}")
    public int add4(@PathVariable String name){
        int i = 0;
        i += service.privateAdd1(name);
        i += service.privateAdd2(name);
        return i;
    }
}
